package cn.yuanqiao.cqbc.dataManage.service.impl;
/**
 * @author 赵常飞
 * @date 2024/6/13 15:44
 */

import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.cqbc.dataManage.domain.DataManage;
import cn.yuanqiao.cqbc.dataManage.mapper.DataManageMapper;
import cn.yuanqiao.cqbc.dataManage.service.IDataManageService;
import cn.yuanqiao.feignService.RemoteCommonService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@Author: 赵常飞
 *@Date: 2024/6/13 15:44
 *@Description:
 *@Note:
 */
@Service
public class DataManageServiceImpl implements IDataManageService {
    @Autowired
    private DataManageMapper dataManageMapper;
    @Autowired
    private RemoteCommonService remoteCommonService;
    @Value("${HD_RECEIVE_PATH}")
    private String hdReceivePath;
    @Override
    public List<DataManage> selectList(DataManage dataManage) {
        dataManage.setIsdel("0");
        dataManage.setTenantCode(SecurityUtils.getLoginUser().getSysUser().getTenantCode());
        return dataManageMapper.selectList(dataManage);
    }

    @Override
    public DataManage getDataManagebyId(Long id) {
        return dataManageMapper.getDataManagebyId(id);
    }

    @Override
    public int update(DataManage dataManage) {
        return dataManageMapper.update(dataManage);
    }

    @Override
    public int delete(Long id) {
        DataManage dataManage=new DataManage();
        dataManage.setId(id);
        dataManage.setIsdel("1");
        return dataManageMapper.update(dataManage);
    }

    @Override
    public int updatedatable(Long id, String memorypool) {
        return dataManageMapper.updatedatable(id,memorypool);
    }

    @Override
    public String fourCheck(JSONObject tranferjson) {
        JSONObject json=new JSONObject(1<<3);
        json.put("path",tranferjson.getString("path")+ File.separator+tranferjson.getString("name")+"."+tranferjson.getString("format"));
        json.put("code",tranferjson.getString("code"));
        json.put("link","2");
        json.put("lx","1");
        AjaxResult data = remoteCommonService.detectionPackage(json);
        int  failNum= (Integer) data.get("failNum");
        String sxId=(String) data.get("sxId");
        DataManage dataManage=new DataManage();
        dataManage.setId(tranferjson.getLong("id"));
        dataManage.setSixingDetail(sxId);
        if(failNum>0){
            dataManage.setSixing("400");
            dataManageMapper.update(dataManage);
            return "四性检测未通过!";
        }else{
            dataManage.setSixing("200");
            dataManageMapper.update(dataManage);
            return "四性检测通过!";
        }
    }

    @Override
    public int handadd(Long categoryId,String name, String size, String receivingTime, String format, String remark, String saveTime, MultipartFile file) {
        String uplodPath = hdReceivePath;
        if (StringUtils.isEmpty(uplodPath)) {
            throw new ServiceException("系统参数错误YQ_MNT未找到配置！");
        }
        String absPath ="";
        try {
            absPath = uplodPath + File.separator + name+"."+format;
            File desc = new File(absPath);
            if (!desc.exists()) {
                if (!desc.getParentFile().exists()) {
                    desc.getParentFile().mkdirs();
                }
            }
            file.transferTo(Paths.get(absPath));
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

        String tenantCode=SecurityUtils.getLoginUser().getSysUser().getTenantCode();
        Map<String,String> category=dataManageMapper.getfondsbycategoryId(categoryId,tenantCode+"_CATEGORY");
        DataManage dataManage=new DataManage();
        dataManage.setId(SnowflakeIdGenerator.generateId());
        dataManage.setName(name);
        dataManage.setSize(size);
        dataManage.setReceivingTime(receivingTime);
        dataManage.setFormat(format);
        dataManage.setREMARK(remark);
        dataManage.setSaveTime(saveTime);
        dataManage.setFonds(String.valueOf(category.get("FONDS_CODE")));
        dataManage.setCategory(categoryId);
        dataManage.setIsdel("0");
        dataManage.setTitle(name);
        dataManage.setDh(name);
        dataManage.setPath(absPath);
        dataManage.setTenantCode(tenantCode);
        dataManage.setZlfs(String.valueOf(category.get("TYPE")));
        dataManage.setBiaoshi(-1L);
        return dataManageMapper.insert(dataManage);
    }
}
